package com.sghd.fight.module.game.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.fight.module.game.service.skill.effect.Effect;
import com.sghd.fight.module.game.service.skill.effect.EffectType;
import com.sghd.pojo.fight.game.ChessType;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author frank
 * @date 2018-10-22 17:47
 * @desc 技能配置
 **/
@Entity
@Table(catalog = "bsdld_fight")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_MINUTE))
public class SkillConfig implements IEntity<Integer>, PerPersist {
    /** 技能id*/
    @Id
    private int id;
    /** 技能效果*/
    @Transient
    private Effect effect;
    @Enumerated(EnumType.STRING)
    private EffectType effectType;
    /**技能名字*/
    private String name;
    /** 技能目标(自己/敌方/全体)*/
    private String target;
    /** 消耗回合数*/
    private int round;
    /**公式上下文*/
    @Transient
    private Map<String, Object> context;
    @Lob
    private String contextJson;
    @Transient
    private Map<Integer, Integer> level2Cool;
    @Lob
    private String level2CoolJson;
    /** 对应的五行类型*/
    @Enumerated(EnumType.STRING)
    private ChessType type;
    /** 等级对应的元素消耗<等级，消耗>*/
    @Transient
    private Map<Integer, Map<ChessType, Integer>> level2Expends;
    @Lob
    private String level2ExpendsJson;

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel2ExpendsJson() {
        return level2ExpendsJson;
    }

    public void setLevel2ExpendsJson(String level2ExpendsJson) {
        this.level2ExpendsJson = level2ExpendsJson;
    }

    public Map<Integer, Map<ChessType, Integer>> getLevel2Expends() {
        return level2Expends;
    }

    public void setLevel2Expends(Map<Integer, Map<ChessType, Integer>> level2Expends) {
        this.level2Expends = level2Expends;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    public Effect getEffect() {
        return effect;
    }

    public void setEffect(Effect effect) {
        this.effect = effect;
    }

    public EffectType getEffectType() {
        return effectType;
    }

    public void setEffectType(EffectType effectType) {
        this.effectType = effectType;
    }

    public Map<String, Object> getContext() {
        return context;
    }

    public void setContext(Map<String, Object> context) {
        this.context = context;
    }

    public String getContextJson() {
        return contextJson;
    }

    public void setContextJson(String contextJson) {
        this.contextJson = contextJson;
    }

    public Map<Integer, Integer> getLevel2Cool() {
        return level2Cool;
    }

    void setLevel2Cool(Map<Integer, Integer> level2Cool) {
        this.level2Cool = level2Cool;
    }

    public String getLevel2CoolJson() {
        return level2CoolJson;
    }

    void setLevel2CoolJson(String level2CoolJson) {
        this.level2CoolJson = level2CoolJson;
    }

    @Override
    public void afterLoad() {
        context = JsonUtils.string2Map(contextJson);
        level2Cool = JsonUtils.string2Map(level2CoolJson, Integer.class, Integer.class);
        level2Expends = JsonUtils.string2GenericObject(level2ExpendsJson, new TypeReference<Map<Integer, Map<ChessType, Integer>>>() {
        });
    }

    @Override
    public void beforePersist() {
    }

    @Transient
    public SkillConfig copy() {
        SkillConfig skillConfig = new SkillConfig();
        skillConfig.id = id;
        skillConfig.effect = effect;
        skillConfig.target = target;
        skillConfig.level2Expends = new HashMap<>(level2Expends);
        skillConfig.context = new HashMap<>(context);
        skillConfig.round = round;
        skillConfig.level2Cool = level2Cool;
        skillConfig.type = this.type;
        return skillConfig;
    }

    public ChessType getType() {
        return type;
    }

    public SkillConfig setType(ChessType type) {
        this.type = type;
        return this;
    }

    public int getCool(int level) {
        return level2Cool.get(level);
    }

    public Map<ChessType, Integer> getExpends(int level) {
        Map<ChessType, Integer> expends = level2Expends.get(level);
        if (expends == null) {
            expends = new HashMap<>();
        }
        return expends;
    }
}
